package com.yjps.system.service;


import com.yjps.common.exception.DocumentException;
import com.yjps.common.page.Pager;
import com.yjps.system.dao.RoleDao;
import com.yjps.system.dao.RolePerDao;
import com.yjps.system.model.Permission;
import com.yjps.system.model.Role;
import com.yjps.system.model.RolePermission;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jessy on 2015/7/16.
 */
@Service("roleService")
public class RoleService{
    private Logger log = LoggerFactory.getLogger(RoleService.class);
    @Resource
    private RoleDao roleDao;
    @Resource
    private RolePerDao rolePerDao;

    @Transactional
    public void addRole(Role role) {
        roleDao.save(role);
        log.info("...ɫ " + role.getRolename() + " ...");
        if(role.getPermissions()!= null && role.getPermissions().size() > 0){
            for(Permission permission:role.getPermissions()){
                RolePermission rp = new RolePermission();
                rp.setIsValid(1);
                rp.setRoleId(role.getId());
                rp.setPermissionId(permission.getId());
                rolePerDao.save(rp);
            }
        }
    }

    @Transactional
    public void deleteRole(int id) {
        roleDao.deleteById(id);
        log.info(".... " + id + "....");
    }

    @Transactional
    public Role updateRole(Role role) {
        try {
            String hql = "update Role r set r.rolename =:rolename,r.role_description =:role_description where r.id =:id";
            Map<String, Object> map = new HashMap<>();
            map.put("rolename",role.getRolename());
            map.put("role_description",role.getRole_description());
            map.put("id",role.getId());
            roleDao.excute(hql, map);
            BeanUtils.copyProperties(role, roleDao.load(role.getId()));

        }catch (NullPointerException e){
            throw new DocumentException("");
        } catch (InvocationTargetException e) {
            throw new DocumentException("");
        } catch (IllegalAccessException e) {
            throw new DocumentException("");
        }
        return role;
    }

    public List<Role> roleList() {
        String hql = "from Role r";
        return roleDao.find(hql);
    }
    public Pager<Role> getRoleByPager(Pager<Role> pager) throws Exception {
        Map<String, Object> params = new HashMap<>();
        String queryString = "from Role t where t.isValid = 1";
        queryString += " order by t.id desc";
        List<Role> accounts = roleDao.findWithSelect(queryString, params, (pager.getPageNo()-1)*pager.getPageSize(), pager.getPageSize());
        pager.setCount(roleDao.getTotalCount(queryString, params));
        pager.setList(accounts);
        return pager;
    }


    public Role findRole(int id) {
        return roleDao.load(id);
    }
    public Role findRoleByName(String rolename) {
        String hql = "from Role r where r.rolename = :rolename";
        Map<String, Object> map = new HashMap<>();
        map.put("rolename",rolename);
        return (Role)roleDao.findUniqueResult(hql, map);
    }

}
